using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaySound : MonoBehaviour
{
    public AudioClip[] audios;
    public AudioSource audioSource;
    static public int soundLibId = 0;
    static public float delayTime = 1f;

    float[] waitLine = new float[100];
    int[] idInLine = new int[100];
    int index = 0, readIndex = 0;
    int waiting = 0;

    public void PlayWithId(int id)
    {
        waiting++;
        waitLine[index] = Time.realtimeSinceStartup + delayTime;
        idInLine[index] = id;
        index++;
        index %= 100;
    }

    void Update()
    {
        if(waiting > 0)
        {
            if(Time.realtimeSinceStartup >= waitLine[readIndex])
            {
                int xl = 2 - idInLine[readIndex] / 7, lx = idInLine[readIndex] % 7;
                string name = xl.ToString() + lx.ToString();
                GameObject.Find(name).GetComponent<AudioSource>().Stop();
                GameObject.Find(name).GetComponent<AudioSource>().PlayOneShot(audios[idInLine[readIndex] + soundLibId * 21]);
                readIndex++;
                waiting--;
                readIndex %= 100;
            }
        }
    }
}
